﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SubSonic;
using LinFen;

public partial class webUserControl_AdminUserAdd : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
      
        if(!IsPostBack)
        {
            AddItems(ddlCityID, "sysCityName", "0", 0);
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Query q = new Query("adminInfo");
        q.AddWhere("adminName", tbAdminName.Text);
        if(q.GetRecordCount()==0)
        {
            string isType = ddlisType.SelectedValue;
            AdminInfo.Insert(int.Parse(ddlCityID.SelectedValue), 0, tbAdminName.Text, tbPassWD.Text, int.Parse(ddlIsLevel.SelectedValue),0,int.Parse(isType));
            Literal1.Text = "<script language=javascript>alert('添加成功！')</script>";
            Response.Redirect(Request.Url.ToString());
        }
        else
        {
            Literal1.Text = "<script language=javascript>alert('用户已存在！')</script>";
 
        }
       
    }
    static void AddItems(DropDownList ddl, string tb, string id, int isType)
    {
        if (isType == 1)
        {
            for (int i = 0; i <= 10; i++)
            {
                ListItem li = new ListItem();
                li.Value = i.ToString();
                li.Text = i.ToString();
                if (id == i.ToString())
                {
                    li.Selected = true;
                }
                ddl.Items.Add(li);
            }
        }
        else
        {
            Query q = new Query(tb);
            IDataReader dr = q.ExecuteReader();
            while (dr.Read())
            {
                ListItem li = new ListItem();
                li.Value = dr[0].ToString();
                li.Text = dr[1].ToString();
                if (id == dr[0].ToString())
                {
                    li.Selected = true;
                }
                ddl.Items.Add(li);
            }
        }

    }

    protected void ddlIsLevel_SelectedIndexChanged(object sender, EventArgs e)
    {
        if(ddlIsLevel.SelectedValue=="1")
        {
            Panel2.Visible = false;
        }
        else
        {
            Panel2.Visible = true;
        }
    }
    protected void ddlisType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if(ddlisType.SelectedValue=="5")
        {
            Panel2.Visible = true;
            ddlIsLevel.Enabled = true;
        }
        if(ddlisType.SelectedValue=="4")
        {
            Panel2.Visible = true;
            ddlIsLevel.Enabled = true;
        }
        if (ddlisType.SelectedValue == "3")
        {
            Panel2.Visible = true;
            ddlIsLevel.Enabled = false;
            ddlIsLevel.SelectedIndex = 0;
        }
        if(ddlisType.SelectedValue=="2")
        {
            ddlIsLevel.SelectedIndex = 1;
            ddlIsLevel.Enabled = false;
            Panel2.Visible = true;
        }
        if (ddlisType.SelectedValue == "1")
        {
            Panel2.Visible = false;
            ddlIsLevel.Enabled = true;
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DEL")
        {
            AdminInfo.Delete("id", e.CommandArgument.ToString());
            GridView1.DataBind();
        }
    }
    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DEL")
        {
            AdminInfo.Delete("id", e.CommandArgument.ToString());
            GridView1.DataBind();
        }
    }
}
